package com.example.wan1.view.welcome;

import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.wan1.R;
import com.example.wan1.base.activity.BaseActivity;
import com.example.wan1.utils.ScreenUtils;
import com.example.wan1.view.web.JumpTool;

import butterknife.BindView;

/***
 * 创建时间：2020/8/9 17:42
 * 创建人：10850
 * 功能描述：欢迎页面
 */
public class WelcomeActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.jump_btn)
    Button mJumpbtn;

    private boolean isGoMain = false;

    private CountDownTimer countDownTimer;//计时器

    @Override
    protected int initLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    protected void initData() {
//        countDownTimer = new MyCountDownTimer(5000 + 200, 1000);
        countDownTimer = new MyCountDownTimer(3000 + 200, 1000);
        countDownTimer.start();
        mJumpbtn.setVisibility(View.VISIBLE);
    }

    @Override
    protected void initView() {
        ScreenUtils.hideBottomUIMenu(WelcomeActivity.this);//隐藏底部状态栏
        mJumpbtn.setOnClickListener(this);
    }

    //跳到主页
    private void goMainActivity() {
        if (isGoMain) return;
        JumpTool.goHome(this);
        finish();
        isGoMain = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    /***
     * 指的是这个Activity得到或者失去焦点的时候，会被调用
     * 也就是说 如果你想要做一个Activity一加载完毕，就触发什么的话 完全可以用这个！！！
     * @param hasFocus
     */
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            ScreenUtils.hideBottomUIMenu(WelcomeActivity.this);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.jump_btn) {
            goMainActivity();
        }
    }


    /**
     * 倒计时计时器
     */
    private class MyCountDownTimer extends CountDownTimer {

        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            mJumpbtn.setText("跳过(" + millisUntilFinished / 1000 + "s)");
            //Log.i("zxd", "onTick: " + millisUntilFinished + "\t" + millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            mJumpbtn.setText("跳过(" + 0 + "s)");
            goMainActivity();
        }
    }
}
